$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'xinpai/car/list',
        datatype: "json",
        colModel: [
            { label: '车辆IMEI', name: 'imei', index: 'imei', width: 80,key: true},
			{ label: '魔盒ID', name: 'cId', index: 'c_id', width: 50 },
            { label: '内部编号', name: 'interNum', index: 'inter_num', width: 80 },
            { label: '车牌号', name: 'plateNum', index: 'plate_num', width: 80 },
            { label: '车辆型号', name: 'modelNum', index: 'model_num', width: 80 },
			{ label: '手机卡号', name: 'sim', index: 'sim', width: 80 },
			{ label: '车辆类型', name: 'type', index: 'type', width: 80,formatter:function (value) {
			    for(let  key in vm.dict.type){
                    for (let j = 0; j < vm.dict.type[key].length; j++) {
                        var obj =vm.dict.type[key][j];
                        if(obj.code==parseInt(value)){
                            return obj.value;
                        }
                    }
                }
            }},
            { label: '底盘类型', name: 'underpanType', index: 'underpan_type', width: 80,formatter:function (value) {
                for (var i = 0; i < vm.dict.underpanType.length; i++) {
                    var obj = vm.dict.underpanType[i];
                    if(obj.code==parseInt(value)){
                        return obj.value;
                    }
                }
            }},
            //{ label: '车辆状态', name: 'state', index: 'state', width: 80 },
/*            { label: '出厂日期', name: 'dateManufacture', index: 'date_manufacture', width: 80 },
			{ label: '购买日期', name: 'purchasDate', index: 'purchas_date', width: 80 }, 			
			{ label: '行驶证有效期', name: 'drivingLicense', index: 'driving_license', width: 80 }, 			
			{ label: '道路运输证编号', name: 'transportLicense', index: 'transport_license', width: 80 },
			{ label: '改装厂商', name: 'vender', index: 'vender', width: 80 }, 			
			{ label: '发动机号', name: 'engineOn', index: 'engine_on', width: 80 }, 			
			{ label: '车架号', name: 'chassisOn', index: 'chassis_on', width: 80 },
			{ label: '底盘品牌', name: 'underpan', index: 'underpan', width: 80 }, 			
			{ label: '上装容积', name: 'cubage', index: 'cubage', width: 80 }, 			
			{ label: '负责人ID', name: 'leadingId', index: 'leading_id', width: 80 }, 			
			{ label: '驾驶员ID', name: 'driverId', index: 'driver_id', width: 80 },
			{ label: '燃料种类', name: 'fuelType', index: 'fuel_type', width: 80 }, 			
			{ label: '所属项目ID', name: 'projectId', index: 'project_id', width: 80 }*/
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
function isLicenseNo(str) {
    return /(^[\u4E00-\u9FA5]{1}[A-Z0-9]{6}$)|(^[A-Z]{2}[A-Z0-9]{2}[A-Z0-9\u4E00-\u9FA5]{1}[A-Z0-9]{4}$)|(^[\u4E00-\u9FA5]{1}[A-Z0-9]{5}[挂学警军港澳]{1}$)|(^[A-Z]{2}[0-9]{5}$)|(^(08|38){1}[A-Z0-9]{4}[A-Z0-9挂学警军港澳]{1}$)/.test(str);
}

var vm = new Vue({
	el:'#tjapp',
	data:{
        q:{
            username: null,
        },
		showList: true,
		title: null,
		car: {areaList:[]},
		projectList:[],
        areaList:[],
		userList:{driverId:[],leadingId:[]},
		dict:{type:[],underpanType:[],fuelType:[]}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.car = {projectId:0,leadingId:0,driverId:0,underpanType:0,areaList:[],fuelType:0,type:0};
        },

		update: function (event) {
			var cId = getSelectedRow();
			if(cId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(cId)
		},
		saveOrUpdate: function (event) {
			var url = vm.title == "新增" ? "xinpai/car/save" : "xinpai/car/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.car),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var cIds = getSelectedRows();
			if(cIds == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "xinpai/car/delete",
                    contentType: "application/json",
				    data: JSON.stringify(cIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(cId){
			$.get(baseURL + "xinpai/car/info/"+cId, function(r){
                vm.car = r.car;
                vm.selectInfo();
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			var postData=null;
			if(vm.q.username!=null&&vm.q.username!=""){
                postData={}
                if(vm.q.username.length>13){
                    postData.imei=vm.q.username
                }else{
                     postData.plateNum=vm.q.username
                }
            }
            console.log(postData);
            $("#jqGrid").jqGrid('setGridParam',{
                postData,
                page:page
            }).trigger("reloadGrid");
		},
		selectProjectList:function (data={parentId:0,type:0}) {
            $.ajax({
                type: "GET",
                url:  baseURL + 'xinpai/project/list',
                contentType: "application/json",
                data: data,
                success: function(r){
                    if(r.code === 0){
                    	if(data.parentId>0){
                            vm.areaList=r.list;
						}else{
                            vm.projectList=r.list;
						}

                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        selectInfo:function () {
			if(this.car.projectId>0){
                this.selectProjectList({parentId:this.car.projectId,type:1});
			}

        },
		selectDictList:function (name,type) {
            $.ajax({
                type: "GET",
                url:  baseURL + 'sys/dict/select',
                contentType: "application/json",
                data: {name},
                success: function(r){
                    if(r.code === 0){
                        for (let key in vm.dict) {
							if(key==type){
                                vm.dict[key]=r.list;
                            }
                        }
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
		selectUsersList:function (name,type) {
            $.ajax({
                type: "GET",
                url:  baseURL + 'xinpai/users/select',
                contentType: "application/json",
                data: {name},
                success: function(r){
                    if(r.code === 0){
						vm.userList[type]=r.list;
					} else{
                        alert(r.msg);
                    }
                }
            });
        }
    },created:function () {
        this.selectProjectList();
        this.selectDictList("车辆类型",'type')
        this.selectDictList("底盘类型",'underpanType')
        this.selectDictList("燃料种类",'fuelType')
        this.selectUsersList("驾驶员",'driverId')
        this.selectUsersList("车队队长",'leadingId')
    }
});